package com.gzmu.storage.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.gzmu.storage.config.MyWebLog;
import com.gzmu.storage.entity.*;
import com.gzmu.storage.service.CabinetService;
import com.gzmu.storage.service.ChestService;
import com.gzmu.storage.service.RecordService;
import com.gzmu.storage.service.StationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping("/")
public class PageController {
    @Autowired
    private StationService stationService;
    @Autowired
    private CabinetService cabinetService;
    @Autowired
    private ChestService chestService;
    @Autowired
    private RecordService recordService;

    @RequestMapping("index")
    @MyWebLog( name="index", intoDb = true)
    public String index() {
        return "/index";
    }

    @RequestMapping("main")
    @MyWebLog( name="main", intoDb = true)
    public String main() {
        return "/main";
    }

    @RequestMapping("/page/fileQuery/query")
    @MyWebLog( name="main", intoDb = true)
    public String fileQueryquery() {
        return "/page/fileQuery/query";
    }

    @RequestMapping("/page/cabinetManage/manage")
    @MyWebLog( name="main", intoDb = true)
    public String cabinetManagemanage() {
        return "/page/cabinetManage/manage";
    }

    @RequestMapping("/page/cabinetManage/add")
    @MyWebLog( name="main", intoDb = true)
    public String cabinetManageadd(@RequestParam Map<String, String> params, Model model) {
        String type = params.get( "type" );
        String id = params.get( "id" );

        List<Cabinet> cabinets;
        List<Station> stations = null;
        if (type.equals( "station" ) && id != null){
            Station station = stationService.selectById( id );
            model.addAttribute( "station", station );
        }else if(type.equals( "cabinet" )){
            Cabinet cabinet = cabinetService.selectById( id );
            List<Station> station;
            if (cabinet != null){
                station = stationService.selectList(new EntityWrapper<Station>()
                        .eq("id", cabinet.getStationid()));
            } else{
                station = stationService.selectList(null);
            }
            model.addAttribute( "station", station );
            model.addAttribute( "cabinet", cabinet );
        }else if(type.equals( "chest" )){
            Chest chest = chestService.selectById( id );
            if (chest !=null){
                cabinets = cabinetService.selectList( new EntityWrapper<Cabinet>()
                        .eq("id", chest.getCabinetid()) );
                if (cabinets.size() > 0)
                    stations = stationService.selectList(new EntityWrapper<Station>()
                        .eq("id", cabinets.get( 0 ).getStationid()));
            }else{
                stations = stationService.selectList(null);
                if (stations.size()>0){
                    cabinets = cabinetService.selectList( new EntityWrapper<Cabinet>()
                            .eq("stationid", stations.get( 0 ).getId()) );
                    model.addAttribute( "cabinet", cabinets );
                }
            }
            model.addAttribute( "chest", chest );
            model.addAttribute( "station", stations );
        }else{
            stations = stationService.selectList(null);
            if (stations.size() > 0)
                model.addAttribute( "station", stations.get( 0 ) );
        }
        return "/page/cabinetManage/add";
    }

    @RequestMapping("/page/filesManage/fileManage")
    @MyWebLog( name="main", intoDb = true)
    public String filesManagefileManage() {
        return "/page/filesManage/fileManage";
    }

    @RequestMapping("/page/filesManage/add")
    @MyWebLog( name="main", intoDb = true)
    public String filesManageeadd(Integer id, Model model) {
        Record record;
        if (id !=null ){
            record = recordService.selectById( id );
            List<Station> stations = stationService.selectList( new EntityWrapper<Station>()
                    .eq("id", record.getStationid()) );
            model.addAttribute( "stations", stations );

            List<Cabinet> cabinets = cabinetService.selectList( new EntityWrapper<Cabinet>()
                    .eq("stationid", record.getStationid()) );
            model.addAttribute( "cabinets", cabinets );

            List<Chest> chests = chestService.selectList( new EntityWrapper<Chest>()
                    .eq("cabinetid", record.getCabinetid()) );
            model.addAttribute( "chests", chests );

            model.addAttribute( "record", record );
        }else{
            List<Station> stations = stationService.selectList( null );
            model.addAttribute( "stations", stations );
            if (stations.size()>0){
                List<Cabinet> cabinets = cabinetService.selectList( new EntityWrapper<Cabinet>()
                        .eq("stationid", stations.get( 0 ).getId()) );
                model.addAttribute( "cabinets", cabinets );
                if (cabinets.size()>0){
                    List<Chest> chests = chestService.selectList( new EntityWrapper<Chest>()
                            .eq("cabinetid", cabinets.get( 0 ).getId()) );
                    model.addAttribute( "chests", chests );
                }
            }
        }
        return "/page/filesManage/add";
    }

    @RequestMapping("/page/loginLog/log")
    @MyWebLog( name="main", intoDb = true)
    public String loginLoglog() {
        return "/page/loginLog/log";
    }

    @ResponseBody
    @RequestMapping(value = "/page/getTreeData", produces = "application/json;charset=UTF-8")
    public Object getTreeData() {
        Map<String, Object> map = new HashMap<>();
        Integer index = 1;

        List<TreeData> treeDatas = new ArrayList<>();
        List<Station> stations = stationService.selectList( null );
        int stationIndex, cabinetIndex;
        for (Station station : stations){
            EntityWrapper stationsWrapper = new EntityWrapper(  );
            stationsWrapper.eq( "stationid",station.getId() );
            List<Cabinet> cabinets = cabinetService.selectList(stationsWrapper);
            TreeData stationData = new TreeData();
            stationIndex = index++;
            stationData.setId( stationIndex );
            stationData.setName( station.getName() );
            stationData.setType( 1 );
            stationData.setPid( -1 );
            stationData.setSerial( station.getSerial() );
            stationData.setLibid( station.getId() );
            stationData.setUpdatetime( station.getUpdatetime() );
            int stationmax = 0, stationcur = 0, bstation = 0;
            for(Cabinet cabinet : cabinets){
                TreeData cabinetData = new TreeData();
                cabinetIndex = index++;
                cabinetData.setId(cabinetIndex);
                cabinetData.setName( cabinet.getName() );
                cabinetData.setType( 2 );
                cabinetData.setLibid( cabinet.getId() );
                cabinetData.setSerial( cabinet.getSerial() );
                cabinetData.setPid( stationIndex );
                cabinetData.setUpdatetime( cabinet.getUpdatetime() );
                EntityWrapper cabinetsWrapper = new EntityWrapper( );
                cabinetsWrapper.eq( "cabinetid",cabinet.getId() );
                List<Chest> chests = chestService.selectList( cabinetsWrapper );
                int cabinetmax = 0, cabinetcur = 0, bcabinet = 0;
                for(Chest chest: chests){
                    TreeData treeData = new TreeData();
                    treeData.setId(index++);
                    treeData.setName( chest.getName() );
                    treeData.setType( 3 );
                    treeData.setLibid( chest.getId() );
                    treeData.setSerial( chest.getSerial() );
                    treeData.setPid( cabinetIndex );
                    treeData.setUpdatetime( chest.getUpdatetime() );
                    int max = chest.getIntakemax();
                    int cur = chest.getIntakecur();
                    cabinetmax += max;
                    cabinetcur += cur;
                    stationmax += max;
                    stationcur += cur;
                    treeData.setIntakemax( max );
                    treeData.setIntakecur(cur );
                    treeDatas.add( treeData );
                    if (max != cur){
                        treeData.setStatus( "checked" );
                    } else{
                        bcabinet++;
                    }
                }
                if (bcabinet < chests.size()){
                    cabinetData.setStatus( "checked" );
                } else{
                    bstation++;
                }

                cabinetData.setIntakemax( cabinetmax );
                cabinetData.setIntakecur( cabinetcur );
                treeDatas.add( cabinetData );
            }
            if (bstation < cabinets.size()) {
                stationData.setStatus( "checked" );
            }
            stationData.setIntakemax( stationmax );
            stationData.setIntakecur( stationcur );
            treeDatas.add( stationData );
        }
        map.put( "data", treeDatas );
        Object object = JSONObject.toJSON(map);
        System.out.println("======================getTreeData:" + object.toString());
        return object;
    }
}
